package com.health.userbackstage.web.control;


import com.github.pagehelper.PageInfo;
import com.health.userbackstage.pojo.CheckGroup;
import com.health.userbackstage.pojo.CheckItem;
import com.health.userbackstage.pojo.result.Result;
import com.health.userbackstage.pojo.result.ResultEnum;
import com.health.userbackstage.service.CheckGroupService;
import com.health.userbackstage.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Autowired
    private CheckGroupService checkGroupService;

    @Autowired
    private CheckItemService checkItemService;

    @GetMapping("{queryString}")  //查询单个相关
    public Result findOne(@PathVariable("queryString") String queryString) {
        CheckItem checkItem = new CheckItem();
        checkItem.setCode(queryString);
        CheckItem one = checkItemService.selectOneByCode(checkItem);
        List<CheckItem> list = new ArrayList<>();
        if (one == null) {
            checkItem.setCode(null);
            checkItem.setName(queryString);
            CheckItem two = checkItemService.selectOneByName(checkItem);
            if (two != null) {
                list.add(two);
            }
        } else {
            list.add(one);
        }
        return Result.success(list);
    }

    @RequestMapping("/select")//分页查询相关方法
    public Result findAll(Integer currentPage, Integer pageSize, String queryString) {
        PageInfo<CheckItem> checkitems = checkItemService.findPage(currentPage, pageSize);

        return Result.success(checkitems);
    }

    @DeleteMapping("/{id}")//删除相关方法
    public Result del(@PathVariable("id") Integer id) {
        boolean flag = checkItemService.removeById(id);
        if (flag) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }


    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {

        Result error = CheckItemController.error(checkItem);
        if (error.getCode()==400){
            return error;
        }

        int add = checkItemService.add(checkItem);

        if (add != 0) {
            return Result.success("添加成功");
        } else {
            return Result.fail("添加失败");
        }
    }

    @PutMapping("/update")
    public Result update(@RequestBody CheckItem checkItem) {
        int update = checkItemService.update(checkItem);
        if (update!=0){
            return Result.success("修改成功");
        }else{
            return Result.fail(ResultEnum.FAILED_EDIT);
        }
    }


    public static Result error(CheckItem checkItem){

        //判断编码
        if (checkItem.getCode() == null ){
            return Result.fail("请输入编码");
        }

        //判断名称
        if (checkItem.getName() == null ){
            return Result.fail("请输入名称");
        }

        //判断性别
        if (checkItem.getSex() == null ){
            return Result.fail("请输入性别");
        }

        //判断助记码
        if (checkItem.getAge() == null ){
            return Result.fail("请输入助记码");
        }

        //判断说明
        if (checkItem.getRemark() == null ){
            return Result.fail("请输入说明");
        }

        //判断操作
        if (checkItem.getAttention() == null ){
            return Result.fail("请输入操作");
        }

        return Result.success();
    }

}